<?php
/**
* This class manages all tables that
* @author Juan G. Bernal <juancho2986@gmail.com>
* @version 1.0
* @package Tourney
*/
class Tourney{
    private $dbinterface;

    /**
    * Constructor function
    */
    function __construct()
    {
        $this->dbinterface = new DBInterface();
    }

    /**
    * Create a new tourney
    * @param Array $data An associative array with the tourney data
    * @return Bool Indicates if the insertion was succesful
    */
    function create($data)
    {
        $data_trn = array();
        $data_trn['trn_date_begin'] = str_replace('.','-',$data['date_begin']);
        $data_trn['trn_date_end'] = str_replace('.','-',$data['date_end']);
        $data_trn['trn_num_rounds'] = $data['num_rounds'];
        $data_trn['trn_num_delegations'] = pow(2,$data['num_rounds']);
        if($this->dbinterface->exec_insert($data_trn,'tourney'))
        {
            $places = $data['places'];
            foreach($places as $pla)
            {
                $data_plc = array();
                $data_plc['plc_name'] = $pla;
                $this->dbinterface->exec_insert($data_plc,'place');
            }
            $this->createRounds($data['num_rounds']);
            $data_del = array();
            $data_del['del_name'] = 'Externa';
            $this->dbinterface->exec_insert($data_del,'delegation');
            return true;
        }
        else
            return false;
    }

    /**
    * Create the rounds
    * @param Integer $rounds Number of rounds
    */
    function createRounds($rounds)
    {
        for($i=1; $i<=$rounds; $i++)
        {
            $data_rnd = array();
            if($i<=($rounds-2))
                $data_rnd['rnd_name'] = "Ronda ".$i;
            elseif($i==($rounds-1))
                $data_rnd['rnd_name'] = "Semifinal";
            elseif($i==$rounds)
            {
                $data_rnd['rnd_name'] = "3er-4to Puesto";
                $this->dbinterface->exec_insert($data_rnd,'round_wp');
                $data_rnd['rnd_name'] = "Final";
            }
            $this->dbinterface->exec_insert($data_rnd,'round_wp');
        }
    }

    /**
    * Modify a single field of a tourney
    * @param String $fld_name Field name
    * @param String $fld_value Field value
    * @return Bool Indicates if the transaction was succesfully
    */
    function modify($fld_name,$fld_value)
    {
        $data = array();
        if($fld_name=='trn_date_begin' || $fld_name=='trn_date_end')
            $fld_value = str_replace('.','-',$fld_value);
        if($fld_name=='trn_num_rounds')
        {
            if(is_numeric($fld_value))
            {
                $this->dbinterface->exec_delete('round_wp');
                $this->dbinterface->exec_sql("ALTER SEQUENCE round_wp_rnd_id_seq RESTART with 1");
                $this->createRounds($fld_value);
                $data['trn_num_delegations'] = pow(2,$fld_value);
            }
        }
        $data[$fld_name] = $fld_value;
        return $this->dbinterface->exec_update($data,'tourney');
    }

    /**
    * Modify a place
    * @param Integer $id Id of the place
    * @param String $name the new name of the place
    * @return Bool Indicates if the transaction was succesfully
    */
    function modifyPlace($id,$name)
    {
        $data = array();
        $data['plc_name'] = $name;
        return $this->dbinterface->exec_update($data,'place','plc_id='.$id);
    }

    /**
    * Adds a new place
    * @param String $name The name of the place
    * @return Bool Indicates if the transaction was succesfully
    */
    function addPlace($name)
    {
        $data['plc_name'] = $name;
        return $this->dbinterface->exec_insert($data,'place');
    }

    /**
    * Delete a place
    * @param Integer $id Id of the place to be deleted
    */
    function removePlace($id)
    {
        $this->dbinterface->exec_delete('place','plc_id='.$id);
    }

    /**
    * Retrieve the information of the tourney
    * @return Array Array with the tourney data
    */
    function getData()
    {
        $fields = array('to_char(trn_date_begin,\'YYYY.MM.DD\') as trn_date_begin', 'to_char(trn_date_end,\'YYYY.MM.DD\') as trn_date_end',
                        'trn_num_rounds','trn_num_delegations');
        $tourney = $this->dbinterface->exec_select($fields,'tourney');
        if($tourney==0)
            return 0;
        else
            return $tourney[0];
    }

    /**
    * Retrieve all places
    * @return Array Array with places data
    */
    function getPlaces()
    {
        return $this->dbinterface->exec_select(array('*'),'place');
    }
}
?>